from models.student import Student
import csv
stus = []
def show_operator():
    print("请选择具体的操作:添加(A)|删除(D)|更新(U)|查询(Q)|列表(L)|退出(E)")

def err_msg(msg):
    print("*"*20,end="")
    print(msg,end="")
    print("*"*20)

def suc_msg(msg):
    print("="*20,end=">")
    print(msg,end="<")
    print("*"*20)

def get_stu(no):
    for stu in stus:
        if stu.no==no:
            return stu
    return None

#加载学生信息
def load():
    with open("student.csv","r",encoding="utf-8") as f:
        reader = csv.DictReader(f)
        for row in reader:
            stu = Student(row["no"],row["name"],row["age"],row["major"])
            stus.append(stu)
#存储学生
def save():
    with open("student.csv","w",encoding="utf-8",newline="") as f:
        fn = ["no","name","age","major"]
        writer = csv.DictWriter(f,fieldnames=fn)
        writer.writeheader()
        for stu in stus:
            writer.writerow({"no":stu.no,"name":stu.name,"age":stu.age,"major":stu.major})

def add():
    #1、输入员工编号
    no = input("请输学号:")
    stu = get_stu(no) #获取员工字典对象
    if stu:
        err_msg("添加的学生存在!不能添加")
        return
    #2、输入其他信息
    name = input("请输入学生姓名:")
    age = int(input("请输入学生年龄:"))
    major = input("请输入学生专业:")
    stu = Student(no,name,age,major)
    stus.append(stu)
    suc_msg("当当当!添加学生成功!")
    save()

def get_stus(name):
    #根据姓名获取
    temps = []
    for stu in stus:
        if stu.name.find(name) !=-1:
            temps.append(stu)
    return temps

def delete():
    no = input("请输入学号:")
    stu = get_stu(no)
    if stu==None:
        err_msg("删除的学生不存在!")
        return
    stus.remove(stu)
    suc_msg("咣咣咣!删除学生成功!")
    save()

def update():
    no = input("请输入学号")
    stu = get_stu(no)
    if stu==None:
        err_msg("更新的学生不存在!")
        return
    name = input(f"请输入学生姓名({stu.name})")
    age = input(f"请输入年龄({stu.age})")
    major = input(f"请输入专业({stu.major})")
    if name:
        stu.name = name
    if age:
        stu.age = int(age)
    if major:
        stu.major = major
    suc_msg("呼呼呼!更新学生成功!")
    save()

def query():
    op = input("输入查询类型:1(根据员工号查询)2(根据姓名查询)")
    if op == "1":
        no = input("请输入学号:")
        stu = get_stu(no)
        if stu:
            Student.show_title()
            print(stu)
        else:
            err_msg("查询的学生不存在哟!")
    elif op == "2":
        name = input("输入要查询的姓名:")
        temps = get_stus(name)
        if len(temps) > 0:
            Student.show_title()
            for s in temps:
                print(s)
        else:
            err_msg("查询的员工不存在哟!")

    else:
        err_msg("查询条件有误")

def find():
    Student.show_title()
    for stu in stus:
        print(stu)

def run():
    load()
    print("欢迎使用我们的学生管理系统!")
    while True:
        show_operator()
        op = input("输入具体的操作:")
        op = op.upper()
        if op=="A":
            add()
        elif op=="D":
            delete()
        elif op=="U":
            update()
        elif op=="Q":
            query()
        elif op=="L":
            find()
        elif op=="E":
            print("希望下次再使用我们的系统!Bye!")
            break
        else:
            err_msg("请选择一个正确的操作")
